package org.core
{	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.system.Capabilities;
	import flash.system.TouchscreenType;
	import flash.utils.describeType;
	

	public class AbcView extends MovieClip
	{
		
		public var displayContent:MovieClip
		/**
		 *view基类，自动注册国际语言包，可实现运行时换皮肤 
		 * 
		 */		
		public function AbcView()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addStageEvent)
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeStageEvent)
		}
		/**
		 *  初始化，私有
		 * @return 
		 * 
		 */		
		protected function init():void{
			
			
			
		}
		
		/**
		 *  更新显示
		 * @return 
		 * 
		 */		
		public function updata():void{
			
			
			
		}
		
		/**
		 * 绑定 视图
		 * @param child
		 * isfull:是否不区分后缀绑定
		 */		
		public function bindView(child:MovieClip,isfull:Boolean=false):void{
			
			if(child.parent==null){
				//FontManager.instance.mapInstance(child);
			}
			
			//清空子集
			for (var i:int = 0; i< this.numChildren;i++ ) {
				this.removeChildAt(i)
			}
			
			var child_names:Array
			
			if(!isfull){
				
				child_names=getPropertyName(this, /(_EB|_DB)$/)
				
			}else{
				
				child_names=getPropertyName(this)
				
			}
			
			for(var i:int=0;i<child_names.length;i++){
				
				var childname:String=child_names[i]
				//trace(childname+"=============================")
				if(childname.indexOf("btn_")!=-1){
					
					var posx:Number=child[childname].x
					
					var posy:Number=child[childname].y
					
					//this[childname]=new ButtonMovieClip(child[childname])
					
					this[childname].x=posx
					
					this[childname].y=posy
					
				}else{
					this[childname]=child[childname]
					/*
					*/
				}
				
			}
			
			while(child.numChildren>0){
				
				var instance:DisplayObject=child.removeChildAt(0)
				
				child_names.indexOf(instance.name)!=-1?this.addChild(this[instance.name]):this.addChild(instance)
				
			}
			//
			//复制属性
			this.transform=child.transform
			
			//递归
			replaceClassAsAbcView(child)
		}
		
		/**
		 * 替换 AbcView 对象
		 * @param child
		 * 
		 */		
		protected function replaceClassAsAbcView(child:MovieClip):void{
			
			var class_names:Array=getPropertyName(this, /(_POP|_PN|_CP)$/)
			
			for(var i:int=0;i<class_names.length;i++){
				
				var childname:String=class_names[i]
				
				this[childname].bindView(child[childname])
				
				this[childname].x=child[childname].x
				
				this[childname].y=child[childname].y
				
				this.addChild(this[childname])
				
			}
			
		}
		
		/**
		 * 对象替换 
		 * @param child
		 * 
		 */		
		public function replaceByInstance(child:Sprite):void{
			
			var names:Array=getPropertyName(this, /_DB$/)
			
			for(var i:int=0;i<names.length;i++){
				
				var childname:String=names[i]
				
				replaceView(this[childname],child[childname])
				
			}
			
		}
		
		/**
		 * 替换 视图 子对象
		 * @param oldChild
		 * @param newChild
		 * 
		 */		
		public function replaceView(oldChild:DisplayObject,newChild:DisplayObject):void{
			
			newChild.transform = oldChild.transform;
			
			if (oldChild is Sprite && newChild is Sprite)
			{
				(newChild as Sprite).buttonMode = (oldChild as Sprite).buttonMode;
			}
			
//			if (oldChild is ButtonMovieClip && newChild is ButtonMovieClip)
//			{
//				(newChild as ButtonMovieClip).button.transform = (oldChild as ButtonMovieClip).button.transform;
//				
//				(newChild as ButtonMovieClip).copyProperty((oldChild as ButtonMovieClip))
//			}
			
			newChild.x=oldChild.x
			
			newChild.y=oldChild.y
			
			this.addChildAt(newChild,this.getChildIndex(oldChild))
			
			this.removeChild(oldChild)
			
			//重新建立引用
			this[oldChild.name]=newChild
			
			//初始化事件
			addStageEvent(null)
			
		}
		
		/**
		 *激活 
		 * 
		 */		
		public function active():void{
			
			mouseChildren=mouseEnabled=true
			
		}
		
		/**
		 *休眠 
		 * 
		 */			
		public	function resting():void{
			
			
			mouseChildren=mouseEnabled=false
			
		}
		
		/**
		 *布局实际宽度
		 * 
		 */				
		public	function getWidth():Number{
			
			
			return width
			
			
		}
		
		/**
		 *布局实际高度
		 * 
		 */			
		public	function getHeight():Number{
			
			return height
			
		}
		
		/**
		 * 查找对象公开属性 集合 
		 * @param filterTarget
		 * @param filter
		 * @return 
		 * 
		 */		
		protected function getPropertyName(child:Sprite, condition:RegExp=null):Array
		{
			var arr_property:Array = [];
			var reg_test:RegExp = condition;
			
			var xml_target:XML = describeType(child);
			var xmlList_variable:XMLList = xml_target.variable;
			for each(var xml_variable:XML in xmlList_variable)
			{
				var str_pro:String = String(xml_variable.@name);
				
				if(reg_test!=null){
					
					if (reg_test.test(str_pro))
					{
						arr_property.push(str_pro);
					}
					
				}else{
					
					arr_property.push(str_pro);
					
				}
				
			}
			
			return arr_property;
			
		}
		
		/**
		 * 添加对象到显示列表事件
		 * @param e
		 * 
		 */		
		protected function addStageEvent(e:Event):void{
			
			init()
			//LanguageManager.instance.registerComponent(this)				
		}
		
		/**
		 * 对象从显示列表中删除 
		 * @param e
		 * 
		 */		
		protected function removeStageEvent(e:Event):void{
			
			//LanguageManager.instance.unregisterComponent(this)
			
		}
		
		/**
		 * 获取对象实例的路径
		 * @param obj 可视化对象
		 * @return 
		 * 
		 */		
		protected function getRoot(obj:DisplayObject):String{
			
			var treePath:String = ""   
			
			function getTreePath(obj:DisplayObject):String{
				
				treePath == ""?treePath = obj.name:treePath = obj.name + "." + treePath
				if (obj.parent == null) { return treePath; }
				if (obj.parent is DisplayObject==false) {
					return treePath;
				}
				return getTreePath(obj.parent)
			}
			
			return getTreePath(obj) 
		}
		
		
		/**
		 * 设置显示对象的舞台位置类型，“normal”不变
		 * 
		 */		
		
		
		
		/**
		 * 记录初始化舞台时显示对象的位置坐标
		 * 
		 */	
		private var  _initPoint:Point
		
		public function set initPoint(value:Point):void{
			_initPoint=value;
			
		}
		
		public function get initPoint():Point{
			return 	_initPoint
		}
		
		
		
		
		/**
		 * 记录初始化舞台时显示对象的位置坐标
		 * 
		 */	
		private var  _initWidth:Number
		
		public function set initWidth(value:Number):void{
			_initWidth=value;
			
		}
		
		public function get initWidth():Number{
			return 	_initWidth
		}
		
		
		private var  _initHeight:Number
		
		
		public function set initHeight(value:Number):void{
			_initHeight=value;
			
		}
		public function get initHeight():Number{
			return 	_initHeight
		}
		
		
		
		/**
		 *是否支持全屏布局   默认为false
		 */		
		
		private var  _fullscreen:Boolean=false
		
		public function set fullscreen(value:Boolean):void{
			_fullscreen=value;
			
		}
		
		public function get fullscreen():Boolean{
			return 	_fullscreen
		}
		
		
		
		
		
		
		
		
		
		
		
	}
}
